package com.example.controller;

import com.example.domain.Lecture;
import com.example.dto.*;
import com.example.service.*;
import com.example.util.Result;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * ClassName:AdminController
 * Package:com.example.controller
 * User:HP
 * Date:2025/8/24
 * Time:下午1:37
 * Author 周东汉
 * Version 1.0
 * Description:
 */


@RestController
@RequestMapping("/api")
@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_SUPER_ADMIN')")
public class AdminController {
    @Autowired
    private LibraryService libraryService;

    @Autowired
    private BookService bookService;

    @Autowired
    private LectureService lectureService;

    @Autowired
    private UserService userService;

    @Autowired
    private CategoryService categoryService;

    @PostMapping("/close")
    public Result closeLibrary(@RequestBody LibraryCloseDTO libraryCloseDTO) {
        return libraryService.closeLibrary(libraryCloseDTO);
    }

    @PostMapping("/open")
    public Result openLibrary(@RequestParam Long id) {
        return libraryService.openLibrary(id);
    }

    @PostMapping("/category/add")
    public Result addCategory(@RequestBody CategoryAddDTO categoryAddDTO) {
        return categoryService.addCategory(categoryAddDTO);
    }

    @PostMapping("/book/add")
    public Result addBook(@RequestBody BookAddDTO bookAddDTO) {
        return bookService.addBook(bookAddDTO);
    }

    @PostMapping("/book/update")
    public Result updateBook(@RequestBody BookUpdateDTO bookUpdateDTO) {
        return bookService.updateBook(bookUpdateDTO);
    }

    @DeleteMapping("/book/delete/")
    public Result deleteBook(@RequestParam Long id) {
        return bookService.removeBook(id);
    }

    @PostMapping("/lecturer/add")
    public Result addLecture(@RequestBody LectureAddDTO lectureAddDTO) {
        return lectureService.addLecture(lectureAddDTO);
    }

    @PostMapping("/lecturer/update")
    public Result updateLecture(@RequestBody LectureUpdateDTO lectureUpdateDTO) {
        return lectureService.updateLecture(lectureUpdateDTO);
    }

    @DeleteMapping("/lecturer/delete/")
    public Result deleteLecture(@RequestParam Long id) {
        return lectureService.deleteLecture(id);
    }

    @GetMapping("/users")
    public Result getUsers() {
        return userService.getUsers();
    }

    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @PostMapping("/user/status")
    public Result changeUserStatus(@RequestParam Long userId, @RequestParam Integer status) {
        return userService.changeUserStatus(userId, status);
    }


}
